package com.kapi.kapiinterface.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kapi.kapiinterface.utils.HttpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kapiclientsdk.model.response.DailyEnglishResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;

@RestController
@RequestMapping("/noParam")
public class InterfaceController {

    /**
     * 随机生成土味情话
     * @return
     */
    @GetMapping("/loveTalk")
    public String randomLoveTalk() {
        return HttpUtil.get("https://api.vvhan.com/api/text/love");
    }

    /**
     * 输出每日一句励志英语
     *
     * @return
     */
    @GetMapping("/dailyEnglish")
    public DailyEnglishResponse dailyEnglish() throws JsonProcessingException {
        String responseJson = HttpUtil.get("https://api.vvhan.com/api/dailyEnglish?type=sj");


            ObjectMapper objectMapper = new ObjectMapper();

            // 提取 "data" 节点并映射为 DailyEnglishResponse 对象
            JsonNode rootNode = objectMapper.readTree(responseJson);
            JsonNode dataNode = rootNode.get("data");

            DailyEnglishResponse dailyEnglishResponse = objectMapper.treeToValue(dataNode, DailyEnglishResponse.class);

            return dailyEnglishResponse;

    }

    /**
     * 获取本地ip
     *
     * @return
     */
    @GetMapping("/getIp")
    public String getIp() throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
//        System.out.println("本机IP地址：" + localHost.getHostAddress());
        return localHost.getHostAddress();
    }



}
